<template>
    <div>
        <h1>登陆页面</h1>

        <el-form
            :model="form"
            status-icon
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
        >
            <el-form-item label="用户名" prop="pass">
                <el-input type="text" v-model="form.user"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="checkPass">
                <el-input type="text" v-model="form.pass"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="login">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
export default {
    data() {
        var checkAge = (rule, value, callback) => {
            if (!value) {
                return callback(new Error("年龄不能为空"));
            }
            setTimeout(() => {
                if (!Number.isInteger(value)) {
                    callback(new Error("请输入数字值"));
                } else {
                    if (value < 18) {
                        callback(new Error("必须年满18岁"));
                    } else {
                        callback();
                    }
                }
            }, 1000);
        };
        var validatePass = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请输入密码"));
            } else {
                if (this.ruleForm.checkPass !== "") {
                    this.$refs.ruleForm.validateField("checkPass");
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请再次输入密码"));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error("两次输入密码不一致!"));
            } else {
                callback();
            }
        };
        return {
            form: {
                user: "admin",
                pass: "admin",
            },
        };
    },
    methods: {
        login() {
            this.$axios
                .post("http://ceshi5.dishait.cn/admin/login", {
                    username: this.form.user,
                    password: this.form.pass,
                })
                .then((res) => {
                    // localStorage.setItem("token", res.data.data.token);
                    this.$store.commit("token", {
                        token: res.data.data.token,
                        user: this.form.user,
                    });
                    this.$router.push({ path: "/tab" });
                })
                .catch((res) => {
                    console.log(res);
                    alert("用户名或者密码错误，登陆失败");
                });
        },
    },
};
</script>
